﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Utilities;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using Microsoft.SharePoint;
using CNRS.CCP;
using CNRS.CCP.Connector;
using Newtonsoft.Json;

namespace CNRS.CCP.Config
{
	public class ClaimsParameters
	{
		#region Static
		/// <summary>
		/// The Singleton Instance of the Farm parameter
		/// </summary>
		private static ClaimsParameters _Parameters;

		/// <summary>
		/// Flag telling us that the fram paramter has been fully loaded 
		/// </summary>
		private bool _Initialized = false;

		private static Config _config;

		#endregion

		#region Properties
		#region Singleton
		/// <summary>
		/// Instance Singleton
		/// </summary>
		public static ClaimsParameters Instance
		{
			get
			{
				if (_Parameters == null)
				{
					_Parameters = new ClaimsParameters();
					if (!_Parameters._Initialized)
					{
						_Parameters = null;
						new Exception("Configuration not initialized yet !");
					}
				}
				return _Parameters;
			}
		}
		#endregion

		#region Claims
		/// <summary>
		/// Activer le debug
		/// </summary>
		public bool Debug
		{
			get
			{
				return _config.Debug;
			}
		}

		/// <summary>
		/// SharePoint Trusted Token Issuer name, this value is mandotory in order to build the claim value
		/// </summary>
		public string ADFSReference
		{
			get
			{
				return _config.ADFSReference;
			}
		}

		/// <summary>
		/// The identity claim type used by the LDAP and external claims
		/// </summary>
		public string IdClaimType
		{
			get
			{
				return _config.IdClaimType;
			}
		}

		public string IdClaimTypeKey
		{
			get
			{
				return _config.IdClaimType.Split('/').Last();
			}
		}
		/// <summary>
		/// Ignored WebApps
		/// </summary>
		public List<string> IgnoredWebApps
		{
			get
			{
				return _config.IgnoredWebApps;
			}
			
		}

		/// <summary>
		/// Claims Elements
		/// </summary>
		public List<ClaimElement> ClaimElements
		{
			get
			{
				return _config.ClaimElements;
			}
			
		}

		/// <summary>
		/// UserElement are parameters used to query user directory (LDAP server)
		/// </summary>
		public List<UserElement> UserElements
		{
			get
			{
				return _config.UserElements;
			}
		}

		
		/// <summary>
		/// Return the user provider used for people search. 
		/// The name of the provider must be userProvider in the parameters file 
		/// </summary>
		public Provider UserProvider
		{
			get;
			private set;
		}

		public Provider GrouperProvider
		{
			get;
			private set;
		}

		public Provider SqlProvider
		{
			get;
			private set;
		}


		#endregion
		#endregion

		#region Constructors
		/// <summary>
		/// Constructeur principal
		/// </summary>
		public ClaimsParameters()
		{
			CreateParametersInstance();
		}
		#endregion

		#region Method
		/// <summary>
		/// Load parameters as singletron 
		/// </summary>
		/// <returns></returns>
		public void CreateParametersInstance()
		{
			string folder = ClaimProvider.PARAMS_PATH;
			string file = folder + ClaimProvider.PARAMS_FILE_CLAIMSPARAM;

			LogHelper.Log(LogCategorie.ConfigLoader, "Loading Claims Parameters...", Microsoft.SharePoint.Administration.TraceSeverity.Monitorable);
			if (!Directory.Exists(folder))
			{
				LogHelper.Log(LogCategorie.ConfigLoader, "Claims Parameters' Folder not found...", Microsoft.SharePoint.Administration.TraceSeverity.Unexpected);
				return;
			}
			if (!File.Exists(file))
			{
				LogHelper.Log(LogCategorie.ConfigLoader, "Claims Parameters' File not found...", Microsoft.SharePoint.Administration.TraceSeverity.Unexpected);
				return;
			}

			try
			{
				string json = System.IO.File.ReadAllText(file);
				_config = JsonConvert.DeserializeObject<Config>(json);

				// set the default user provider
				UserProvider = GetProviderByName("userProvider");

				// set the Grouper VOOT provider value
				GrouperProvider = GetProviderByName("grouper");

				// set the SQL provider
				SqlProvider = GetProviderByName("sql");

				// set the SQL connection string
				//ConnectionString = GetSqlConnection();

				LogHelper.Log(LogCategorie.ConfigLoader, "CNRS Claims Parameters Loaded...", Microsoft.SharePoint.Administration.TraceSeverity.Monitorable);

				_Initialized = true;
			}
			catch(Exception e)
			{
				LogHelper.Log(LogCategorie.ConfigLoader, e.Message, Microsoft.SharePoint.Administration.TraceSeverity.Unexpected);
				return;
			}
		}


		public static Provider GetProviderByName (string providerName)
		{
			return _config.Providers.Find(item => item.Name == providerName);
		}


		#endregion
	}
}
